//////////////////////////////////////////////////////////////////////////
//
// This file is a part of XORT project.
//
// PROJECT:         Xort compiler and virtual machine
// FILE:            BuildinObjectInfo.cpp
// AUTHOR:          Vladimir Gumenuk
// DATE:            17-Apr-2013
// MODIFICATIONS:
//
//
//     Copyright(c) 2014, Vladimir Gumenuk
//     All rights reserved.
//
//     Redistribution and use in source and binary forms, with or without modification, 
//     are permitted provided that the following conditions are met :
//
//     1. Redistributions of source code must retain the above copyright notice, 
//        this list of conditions and the following disclaimer.
//     2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and 
//        the following disclaimer in the documentation and / or other materials provided with the distribution.
//     3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse 
//        or promote products derived from this software without specific prior written permission.
//
//     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
//     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
//     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
//     THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
//     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
//     OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
//     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
//     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
//     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////

#include "xort.h"
#include "BuildinObjectInfo.h"

BuildInObjectInfo::BuildInObjectInfo()
{
    m_dwNumObjects = 0;
    m_pSymbol = NULL;
}

BuildInObjectInfo::~BuildInObjectInfo()
{
    m_Functions.clear();
    m_pSymbol = NULL;
}

int BuildInObjectInfo::Init( symbolref pSymbol )
{
    m_dwNumObjects++;

    if( m_pSymbol )
        return XORT_OK;

    m_pSymbol = pSymbol;

    SymbolListInfo symListInfo;
    GetSymbolListInfo( m_pSymbol->m_listMembers, symListInfo );

    m_Functions.reserve( symListInfo.dwFunctionsCount + 1 );
    dword dwProcsCount = 0;

    SYMBOL_LIST::iterator theIter = m_pSymbol->m_listMembers.begin();
    symbolref pSym = (Symbol*)theIter;
    while( pSym )
    {
        if( pSym->m_SymKind == sk_proc )
        {
            m_Functions.put( (dword)pSym->m_iIndex, pSym );
            dwProcsCount++;
        }
        // else just skip unknown symbol, or any other symbol like var or so

        theIter++;
        pSym = theIter == NULL ? NULL : (Symbol*)theIter;
    }

    m_Functions.set_itemsCount( dwProcsCount + 1 );

    return XORT_OK;
}

void BuildInObjectInfo::Uninit()
{
    if( m_dwNumObjects )
    {
        if( --m_dwNumObjects == 0 )
        {
            m_Functions.clear();
            m_pSymbol = NULL;
        }
    }
}

symbolref BuildInObjectInfo::get_Symbol()
{
    return m_pSymbol; 
}

symbolref BuildInObjectInfo::get_Method( dword dwIdx )
{
    if( dwIdx > m_Functions.count() )
        return NULL;

    symbolref pMethod = m_Functions.get( dwIdx );

    return pMethod;
}
